package kompics.pp;


import java.util.Set;

import se.sics.kompics.ComponentDefinition;
import se.sics.kompics.Handler;
import se.sics.kompics.Positive;
import se.sics.kompics.address.Address;
import se.sics.kompics.network.Network;

public class SimplePeer extends ComponentDefinition {

	private Set<Address> neighborSet;
	private Address self;
	Positive<Network> network = positive(Network.class);

	public SimplePeer() {
		subscribe(handleInit, control);
		subscribe(handlePingMsg, network);
		subscribe(handlePongMsg, network);	
		
	}
		
    Handler<PingMessage> handlePingMsg = new Handler<PingMessage>() {
       public void handle(PingMessage event) {
    	   System.out.println("[Address]: "+self+" received a ping message....");
           trigger(new PongMessage(self, event.getSource()), network );
           System.out.println("[Address]: "+self+" sent a pong  message to "+ event.getSource());
        }
    };
    
    Handler<PongMessage> handlePongMsg = new Handler<PongMessage>() {
        public void handle(PongMessage event) {
            System.out.println("[Address]: "+self+" received a pong message from: "+ event.getSource());
        }
    };
	
    
	Handler<PeerContInit> handleInit = new Handler<PeerContInit>() {
		public void handle(PeerContInit event) {			
			neighborSet = event.getNeighborSet();
			self = event.getSelf();
			System.out.println("Node initialized with "+ + neighborSet.size() +" neighors ");
		}
	};
	
}
